package com.etb.wisdom.eframe.auth.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etb.core.service.ETBService;
import com.etb.core.utils.SequenceUtil;
import com.etb.web.utils.SessionUtil;
import com.etb.web.vo.Party;
import com.etb.wisdom.eframe.auth.AuthConstant;
import com.etb.wisdom.eframe.auth.AuthConstant.AuthType;
import com.etb.wisdom.eframe.auth.AuthConstant.PartyType;
import com.etb.wisdom.eframe.auth.service.AuthService;
import com.etb.wisdom.eframe.auth.service.PartyResService;
import com.etb.wisdom.eframe.model.EtbPartyAuth;
import com.etb.wisdom.eframe.model.EtbUser;
import com.etb.wisdom.eframe.model.OrgEmployee;

@Service
public class AuthServiceImpl extends ETBService implements AuthService {
	
	@Autowired
	private PartyResService partyResService;
	
	@Override
	public Party initParty(EtbUser user) {
		Party party = new Party(user.getId(), AuthConstant.PartyType.user.toString(), user.getLoginname(), user.getUsername());
		//设置角色
		party.setRoleIds(this.getUserRoleIds(user));
		List resourceList = partyResService.getResources(party.getRoleIds());
		Set resourceSet = new HashSet();
		resourceSet.addAll(resourceList);
		resourceList.clear();
		party.setResources(resourceSet);
		return party;
	}
	
	@Override
	public Party initParty(HttpServletRequest request) {
		EtbUser user = new EtbUser();
		user.setLoginname(request.getRemoteUser());
		service.extend(user);
		Party party =  this.initParty(user);
		SessionUtil.setParty(party);
		return party;
	}

	@Override
	public List getMenuData(Object[] roleIds) {
		if(roleIds == null || roleIds.length == 0) {
			return new ArrayList();
		}
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("partyids", roleIds);
		List<Map> menuSequences = service.queryByNamedsql("query.eframe.framework.auth.getMenuSequences", params);
		Set<String> menuIds = new HashSet<String>();
		for(Map sequence : menuSequences) {
			List<String> seqs = SequenceUtil.separate(sequence.get("appmenuseq").toString());
			menuIds.addAll(seqs);
		}
		params.clear();
		params.put("menuIds", menuIds.toArray());
		return service.queryByNamedsql("query.eframe.framework.auth.getMenuData", params);
	}
	
	@Override
	public Set<String> getUserRoleIds(EtbUser user) {
		//用户角色
		EtbPartyAuth authTemplate = new EtbPartyAuth();
		authTemplate.setPartyid(user.getId());
		authTemplate.setPartytype(PartyType.user.toString());
		authTemplate.setAuthtype(AuthType.role.toString());
		List roleIds = service.querySpecifiedProperty(authTemplate, "authid");
		Set roleIdSet = new HashSet();
		roleIdSet.addAll(roleIds);
		//用户关联员工部分
		OrgEmployee emp = new OrgEmployee();
		emp.setUserid(user.getLoginname());
		service.extend(emp);
		if(emp.exist()) {
			//员工角色
			authTemplate = new EtbPartyAuth();
			authTemplate.setPartyid(emp.getId());
			authTemplate.setPartytype(PartyType.employee.toString());
			authTemplate.setAuthtype(AuthType.role.toString());
			roleIdSet.addAll(service.querySpecifiedProperty(authTemplate, "authid"));
			//员工所在岗位及上级岗位
			if(StringUtils.isNotBlank(emp.getPositionid())) {
				authTemplate = new EtbPartyAuth();
				authTemplate.setPartyid(emp.getPositionid());
				authTemplate.setPartytype(PartyType.position.toString());
				authTemplate.setAuthtype(AuthType.role.toString());
				roleIdSet.addAll(service.querySpecifiedProperty(authTemplate, "authid"));
			}
			//用户所在机构角色
			if(StringUtils.isNotBlank(emp.getOrgid())) {
				authTemplate = new EtbPartyAuth();
				authTemplate.setPartyid(emp.getOrgid());
				authTemplate.setPartytype(PartyType.organization.toString());
				authTemplate.setAuthtype(AuthType.role.toString());
				roleIdSet.addAll(service.querySpecifiedProperty(authTemplate, "authid"));
			}
		}
		return roleIdSet;
	}

//	@Override
//	public List getUserRoleIds(EtbUser user) {
//		//用户角色
//		EtbPartyAuth authTemplate = new EtbPartyAuth();
//		authTemplate.setPartyid(user.getId());
//		authTemplate.setPartytype(PartyType.user.toString());
//		authTemplate.setAuthtype(AuthType.role.toString());
//		List roleIds = service.querySpecifiedProperty(authTemplate, "authid");
//		Set roleIdSet = new HashSet();
//		roleIdSet.addAll(roleIds);
//		//用户关联员工部分
//		OrgEmployee emp = new OrgEmployee();
//		emp.setUserid(user.getLoginname());
//		service.extend(emp);
//		if(emp.exist()) {
//			//员工角色
//			authTemplate = new EtbPartyAuth();
//			authTemplate.setPartyid(emp.getId());
//			authTemplate.setPartytype(PartyType.employee.toString());
//			authTemplate.setAuthtype(AuthType.role.toString());
//			roleIdSet.addAll(service.querySpecifiedProperty(authTemplate, "authid"));
//			//员工所在岗位及上级岗位
//			OrgPosition position = new OrgPosition();
//			position.setId(emp.getPositionid());
//			service.extend(position);
//			List<String> positionIds = new ArrayList<String>();
//			while(position.getId() != null) {
//				positionIds.add(position.getId());
////				authTemplate = new EtbPartyAuth();
////				authTemplate.setPartyid(emp.getPositionid());
////				authTemplate.setPartytype(PartyType.position.toString());
////				authTemplate.setAuthtype(AuthType.role.toString());
////				roleIds.addAll(service.queryOnlyProperty(authTemplate, "authid"));
//				if(position.getUplevelid() != null) {
//					OrgPosition uplevelPosition = new OrgPosition();
//					uplevelPosition.setId(position.getUplevelid());
//					service.extend(uplevelPosition);
//					position = uplevelPosition;
//				}
//			}
//			//岗位所在机构角色
//			OrgOrganization org = new OrgOrganization();
//			org.setId(position.getOrgid());
//			service.extend(org);
//			List<String> orgIds = new ArrayList<String>();
//			while(org.getId() != null) {
//				orgIds.add(org.getId());
//				if(org.getUplevelid() != null) {
//					OrgOrganization uplevelOrg = new OrgOrganization();
//					uplevelOrg.setId(org.getUplevelid());
//					service.extend(uplevelOrg);
//					org = uplevelOrg;
//				}
//			}
//			//用户所在机构角色
//			org = new OrgOrganization();
//			org.setId(emp.getOrgid());
//			service.extend(org);
//			while(org.getId() != null) {
////				authTemplate = new EtbPartyAuth();
////				authTemplate.setPartyid(emp.getOrgid());
////				authTemplate.setPartytype(PartyType.organization.toString());
////				authTemplate.setAuthtype(AuthType.role.toString());
////				roleIds.addAll(service.queryOnlyProperty(authTemplate, "authid"));
//				orgIds.add(org.getId());
//				if(org.getUplevelid() != null) {
//					OrgOrganization uplevelOrg = new OrgOrganization();
//					uplevelOrg.setId(org.getUplevelid());
//					service.extend(uplevelOrg);
//					org = uplevelOrg;
//				}
//			}
//		}
//		
//		
//		return roleIds;
//	}
}
